-- @preview-file on clear

-- local MusicPlayer = {}    最简备用方案
-- MusicPlayer.__index = MusicPlayer

-- -- 方法2A：使用 playStream（长音频/背景音乐）
-- if Dora.Audio.playStream then
--     local success = Dora.Audio:playStream("Music/bgm.mp3", true, 1.0)  -- 文件, 循环, 淡入时间
--     if success then
--         print("✅ 使用 Dora.Audio:playStream 成功")
--     end
-- end

-- music_manager.lua
-- 音乐管理器：统一管理游戏音乐

-- 上方勿动，紧急备用方案！！




local MusicManager = {}
MusicManager.__index = MusicManager

-- 音乐状态
local MusicState = {
    STOPPED = "stopped",
    PLAYING = "playing",
    PAUSED = "paused"
}

function MusicManager.new()
    local instance = {
        -- 当前播放的音乐
        currentBGM = nil,
        currentSource = nil,
        
        -- 状态
        state = MusicState.STOPPED,
        
        -- 音量设置
        volume = 0.7,  -- 默认音量70%
        isMuted = false,
        
        -- 音乐文件配置 路径名全在这里
        musicFiles = {
            main_menu = "Music/bgm.mp3",  --主界面的背景音乐
            gameplay = "Music/bgm.mp3",--游戏中的背景音乐
            battle = "Music/bgm.mp3", --战斗音乐
            victory = "Music/bgm.mp3", --胜利音乐
            game_over = "Music/bgm.mp3" --死亡音乐（不一定用得上）
        }
    }
    
    -- 预检查音频模块
    if not Dora or not Dora.Audio then
        print("警告: Dora音频模块未找到，音乐功能不可用")
    end
    
    return setmetatable(instance, MusicManager)
end

-- 播放背景音乐
function MusicManager:playBGM(musicType, loop, fadeTime)
    if not Dora or not Dora.Audio then
        print("音乐播放失败: 音频模块不可用")
        return false
    end
    
    local filename = self.musicFiles[musicType]
    if not filename then
        print("音乐播放失败: 未找到类型 " .. musicType)
        return false
    end
    
    -- 停止当前音乐
    self:stopBGM()
    
    -- 播放新音乐
    local success = false
    
    -- 方法1: 使用Dora.Audio.playStream（推荐）
    if Dora.Audio.playStream then
        success = Dora.Audio:playStream(filename, loop or true, fadeTime or 1.0)
        if success then
            print("播放音乐: " .. musicType .. " (" .. filename .. ")")
            self.state = MusicState.PLAYING
        end
    -- 方法2: 使用Dora.AudioSource (备选方案，确保代码完整)
    elseif Dora.AudioSource then
        local source = Dora.AudioSource(filename, false)
        if source then
            source.looping = loop or true
            source.volume = self.volume
            success = source:playBackground()
            if success then
                self.currentSource = source
                self.state = MusicState.PLAYING
                print("播放音乐: " .. musicType .. " (" .. filename .. ")")
            end
        end
    end
    
    if not success then
        print("音乐播放失败: " .. filename)
        self.state = MusicState.STOPPED
    end
    
    return success
end

-- 停止背景音乐
function MusicManager:stopBGM(fadeTime)
    if self.state == MusicState.STOPPED then
        return
    end
    
    if Dora.Audio and Dora.Audio.stopStream then
        Dora.Audio:stopStream(fadeTime or 0.5)
    elseif self.currentSource then
        self.currentSource:stop(fadeTime or 0.5)
        self.currentSource = nil
    end
    
    self.state = MusicState.STOPPED
    print("停止背景音乐")
end

-- 暂停背景音乐
function MusicManager:pauseBGM()
    if self.state ~= MusicState.PLAYING then
        return
    end
    
    if self.currentSource then
        -- 通过设置音量为0来模拟暂停
        self.currentSource.volume = 0
        self.state = MusicState.PAUSED
        print("暂停背景音乐")
    end
end

-- 恢复背景音乐
function MusicManager:resumeBGM()
    if self.state ~= MusicState.PAUSED then
        return
    end
    
    if self.currentSource then
        self.currentSource.volume = self.volume
        self.state = MusicState.PLAYING
        print("恢复背景音乐")
    end
end

-- 设置音量
function MusicManager:setVolume(volume)
    self.volume = math.max(0, math.min(1, volume or 0.7))
    
    if self.currentSource and self.state == MusicState.PLAYING then
        self.currentSource.volume = self.volume
    end
    
    print("设置音量: " .. self.volume)
end

-- 静音/取消静音
function MusicManager:setMuted(muted)
    self.isMuted = muted
    
    if self.currentSource then
        self.currentSource.volume = muted and 0 or self.volume
    end
    
    print(muted and "音乐已静音" or "取消音乐静音")
end

-- 检查音乐是否正在播放
function MusicManager:isPlaying()
    return self.state == MusicState.PLAYING
end

-- 获取当前音乐状态
function MusicManager:getStatus()
    return {
        state = self.state,
        currentBGM = self.currentBGM,
        volume = self.volume,
        isMuted = self.isMuted
    }
end

return MusicManager